package scu.maqiang.statictics;

import scu.maqiang.fes.FES1L21;
import scu.maqiang.mesh.Mesh1L2;
import scu.maqiang.mesh.ScalarFunc;

public class GammaFunction {
	
	public GammaFunction(double alpha) {
		this.alpha = alpha;
		mesh = new Mesh1L2();
		mesh.line(0, 10, 100000000);
		fs = new FES1L21(mesh);
	}
	
	public double getValue() {
		int intAlpha = (int)alpha;
		double doubleAlpha = alpha - intAlpha;
		double result = 0.0;
		if (Math.abs(doubleAlpha) < 1.0e-10) {
			result = 1;
			doubleAlpha = 1;
		} else {
			double[] param = {doubleAlpha - 1};
			double[] u = mesh.functionInMesh(gfun, param);
			u[0] = 0;
			//System.out.println(u[1]);
			//System.out.println(Arrays.toString(u));
			result = fs.integrate(u);
		}
		for(int i = 0, n = intAlpha - 1; i < n; i++) {
			result *= (doubleAlpha + i);
		}
		return result;
	}
	
	
	public static void main(String[] args) {
		GammaFunction g1 = new GammaFunction(0.4);
		System.out.println(g1.getValue());
	}
	
	private double alpha;
	ScalarFunc gfun = (x, label, param) -> Math.pow(x[0], param[0]) * Math.exp(-x[0]);
	Mesh1L2 mesh;
	FES1L21 fs;
}
